package taller.clienteweb.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import taller.clienteweb.model.BusinessDelegate;
import taller.dataLayer.vo.RodamientoVO;


public class ListarRodamientos extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public ListarRodamientos() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int page = 1;
		// Ver como lo queremos
        int recordsPerPage = 5;
        String pageNumber = request.getParameter("page");
        if(pageNumber != null)
            page = Integer.parseInt(pageNumber);
        
        List<RodamientoVO> rodamientos = BusinessDelegate.getInstance().obtenerListaRodamientos((page-1)*recordsPerPage, recordsPerPage);
        
        int noOfRecords = BusinessDelegate.getInstance().getCantidadRodamientos();
        int noOfPages = (int) Math.ceil(noOfRecords * 1.0 / recordsPerPage);
        request.setAttribute("rodamientos", rodamientos);
        request.setAttribute("noOfPages", noOfPages);
        request.setAttribute("currentPage", page);
        RequestDispatcher dispatcher = request.getRequestDispatcher("listarRodamientos.jsp");
        dispatcher.forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

}
